/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var confirmOrder = function(delivery){
  $.ajax({
      url: '/ajax/order?delivery=' + delivery,
      dataType: 'json'
     }) 
     .done(function(data){
        var n = noty({text: data.message, type: 'info'});
        if(data.status == 1){
          setTimeout(function(){n.close(); window.location.href = "/";}, 3000);
        }
     });
}

var showPromotion = function(show) {
  var elem = $('.promoContent');
  var elemm = $('.promoBg');
  if (!elem.is(':visible')) {
    if (show) {
      elem.show(200);
      elemm.show(200);
    }
  }
  else {
    elem.hide(200);
    elemm.hide(200);
  }
};

var activeNewsletter = function(email){
  $.ajax({
      url: '/ajax/registerNewsletter?newsletter_email=' + email,
      dataType: 'json'
     }) 
     .done(function(data){
        var type = 'warning';
        if(data["status"] === 1){
          type = 'success';
        }
        var n = noty({text: data["message"], type: type});
        setTimeout(function(){n.close();}, 3000);
     });
};

var getActivePage = function(action){
  var url = '/ajax/pages?route=' + action;
  if(action === 'bucket'){
    url = '/ajax/bucket';
  }
  else if(action === 'contact'){
    url = '/ajax/contact';
  }
  else if(action === 'deliveryType'){
    url = '/ajax/deliveryType';
  }
  $.ajax({
      url: url,
      dataType: 'json'
     }) 
     .done(function(data){
        $('.promoContainer').html(data.content);
        showPromotion(true);
     });
};


var executeLogin = function(login, password){
  $.ajax({
      url: '/ajax/login?login=' + login + "&password=" + password,
      dataType: 'json'
     }) 
     .done(function(data){
        var n = noty({text: data.message, type: data.messageType});
        if(data.status == 1){
          setTimeout(function(){n.close();window.location.href = "/";}, 3000);
        }
     });
};

var executeRegisterAccount = function(acc, password, re_password, email, name, surname, adress, telephone, birthday){
  var url = 'account_name=' + acc + '&re_password=' + re_password + '&password=' + password + "&email=" + email + "&name=" + name + 
            '&surname=' + surname + "&adress=" + adress + "&telephone=" + telephone + "&birthday=" + birthday;
  $.ajax({
      url: '/ajax/registerAccount?' + url,
      dataType: 'json'
     }) 
     .done(function(data){
        var n = noty({text: data.message, type: data.messageType});
        if(data.status == 1){
          setTimeout(function(){n.close();window.location.href = "/";}, 3000);
        }
     });
};

var logout = function(){
  $.ajax({
      url: '/ajax/logout',
      dataType: 'json'
     }) 
     .done(function(data){
        var n = noty({text: data.message, type: data.messageType});
        if(data.status == 1){
          setTimeout(function(){n.close();window.location.href = "/";}, 3000);
        }
     });
};

var scrollDown = function(header){
    if(header === false){
      $('.zoneBox').addClass('zoneBoxScroll');
      $('.underButton').addClass('underButtonScroll');
    }
    else{
      if($('.zoneBox').hasClass('zoneBoxScroll')){
        $('.zoneBox').removeClass('zoneBoxScroll');
        $('.underButton').removeClass('underButtonScroll');
      }
    }
    
};


var addProductCount = function(count){
  var count = parseInt($('#productCount').val()) + count;
  if(count < 1){
    count = 1;
  }
  $('#productCount').val(count);
};


var addBucketCount = function(obj, count){
    var obj = $(obj).parent().siblings('.nmb');
    var count = parseInt(obj.html()) + count;
    if(count < 1){
      count = 1;
    }

    var price = parseInt($(this).siblings('.price'));
    var dataid = obj.parent().attr('data-id');
    obj.html(count);  
    $.ajax({
      url: '/ajax/addProductCount?productid=' + dataid + "&count=" + count,
      dataType: 'json'
     }) 
     .done(function(data){
        if(data.status === 1){
        $('#bucketValue').html(data.bucketValue);
      }
      else{
        var n = noty({text: data.message, type: 'warning'});
          setTimeout(function(){n.close();}, 3000);
      }
     });
};

removeProductFromBucket = function(obj){
  var obj = $(obj).parent().parent();
      var id = obj.attr('data-id');
      obj.remove();
      $.ajax({
      url: '/ajax/removeProduct?productid=' + id,
      dataType: 'json'
     }) 
     .done(function(data){
        if(data.status === 1){
          if(data.refresh === 0){
            $('#bucketValue').html(data.bucketValue);
          }
          else{
            window.location = "/";
          }
      }
      else{
        var n = noty({text: data.message, type: 'warning'});
          setTimeout(function(){n.close();}, 3000);
      }
     });
};

var addProduct = function(productid, count){
  $.ajax({
      url: '/ajax/addproduct?productid=' + productid + "&count=" + count,
      dataType: 'json'
     }) 
     .done(function(data){
        var n = noty({text: data.message, type: data.messageType});
        if(data.status == 1){
          setTimeout(function(){n.close();}, 3000);
        }
     });
};